package org.enwey.icarenew;

import android.app.Application;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;

import com.kbeanie.imagechooser.api.FileUtils;
import com.kollway.android.imagecachelib.ImageCacheManager;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.APPUtils.FileUtil;
import org.enwey.icarenew.Manager.SettingsManager;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by enwey on 2014/12/18.
 */
public class MyApplication extends Application {
    public static final Handler uiHandler = new Handler(Looper.getMainLooper());
    public static final ExecutorService bgExecutor = Executors.newCachedThreadPool();
    @Override
    public void onCreate() {
        super.onCreate();
        Constant.init(this);
        SettingsManager.init(this);
        APIManager.getInstance(this);
        initImageCache();
        mkdirs();
    }

    private void initImageCache() {
        ImageCacheManager.init(getApplicationContext());
    }

    private void mkdirs() {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            FileUtil.CAHCHEDIR = Environment.getExternalStorageDirectory() + "/android/data/" + getApplicationContext().getPackageName();
        }else{
            FileUtil.CAHCHEDIR = getApplicationContext().getCacheDir().getPath();//用机身的
        }

        FileUtils.DIRECTORY_CACHE = FileUtil.CAHCHEDIR;

        File file = new File(FileUtil.CAHCHEDIR);
        if(!file.exists()) {
            file.mkdirs();
        }
    }
}
